// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_HR_MONTH_SALARY
// * CreateDate    : 2013-08-28 11:00:56
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:56
// * LastChanger   : 2013-08-28 11:00:56
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Hr
{
/// <summary>
    /// HrMonthSalary
    /// </summary>
    public partial class HrMonthSalaryBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>Entity</returns>
        public static HrMonthSalaryEntity GetHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.SelectSingle<HrMonthSalaryEntity>(objHrMonthSalaryEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.Insert(objHrMonthSalaryEntity);
        }

        public static bool InsertHrMonthSalary(List<HrMonthSalaryEntity> objHrMonthSalaryEntityList)
        {
            return DataAccess.Insert(objHrMonthSalaryEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.Update(objHrMonthSalaryEntity);
        }

        public static bool UpdateHrMonthSalary(List<HrMonthSalaryEntity> objHrMonthSalaryEntityList)
        {
            return DataAccess.Update<HrMonthSalaryEntity>(objHrMonthSalaryEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.Delete(objHrMonthSalaryEntity);
        }

        public static bool DeleteHrMonthSalary(List<HrMonthSalaryEntity> objHrMonthSalaryEntityList)
        {
            return DataAccess.Delete<HrMonthSalaryEntity>(objHrMonthSalaryEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetHrMonthSalaryAll()
        {
            return DataAccess.SelectAllDataSet<HrMonthSalaryEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<HrMonthSalaryEntity> GetHrMonthSalaryAllList()
        {
            return DataAccess.SelectAll<HrMonthSalaryEntity>();
        }
        #endregion

        public static DataTable GetHrMonthSalaryByCondition(HrMonthSalaryEntity salaryCondition, HrEmployeeEntity employeeCondition, string month)
        {
            string sql = @"SELECT P.PERSON_ID AS SELECTED_PERSON, '" + month + @"' AS SALARY_DATE, P.EMPLOYEE_NO, P.EMPLOYEE_NAME,S.EMPLOYEE_NAME AS SUPERVISOR_NAME,
                            P.DEPARTMENT_NO,P.POSITION_ID,
                            MS.*,CONVERT(DECIMAL(18,2),TOTAL_SALARY) AS SALARY,
                            CASE WHEN MS.STATUS IS NULL THEN 'MSS01' ELSE MS.STATUS END AS STATUS_NAME, P.STATUS AS PERSON_STATUS, CONVERT(NVARCHAR(10),p.JOIN_DATE,120) AS JOIN_DATE
                            FROM HR_EMPLOYEE P
                            LEFT JOIN HR_MONTH_SALARY MS ON MS.PERSON_ID = P.EMPLOYEE_NO AND MS.SALARY_MONTH = '" + month + @"'
                            LEFT JOIN HR_EMPLOYEE S ON S.EMPLOYEE_NO = P.SUPERVISOR
                            WHERE 1=1 ";

            if (salaryCondition != null)
            {
                if (!String.IsNullOrEmpty(salaryCondition.Status))
                {
                    sql += " AND MS.STATUS = '" + salaryCondition.Status + "' ";
                }
                if (!String.IsNullOrEmpty(salaryCondition.PersonId))
                {
                    sql += " AND P.EMPLOYEE_NO = '" + salaryCondition.PersonId + "' ";
                }
            }
            if (employeeCondition != null)
            {
                if (!string.IsNullOrEmpty(employeeCondition.Status))
                {
                    sql += " AND P.STATUS = '" + employeeCondition.Status + "' ";
                }

                if (!string.IsNullOrEmpty(employeeCondition.DepartmentNo))
                {
                    sql += " AND p.department_no = '" + employeeCondition.DepartmentNo + "' ";
                }
            }

            sql += " ORDER BY  p.employee_no";
           
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }


        public static bool InsertHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity, List<HrMonthSalaryItemEntity> salaryItems)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (objHrMonthSalaryEntity != null) DataAccess.Insert(objHrMonthSalaryEntity, broker);
                    if (salaryItems != null && salaryItems.Count > 0) DataAccess.Insert(salaryItems, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
           
        }


        public static bool UpdateHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity, List<HrMonthSalaryItemEntity> salaryItems)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    List<HrMonthSalaryItemEntity> delItems = DataAccess.Select(new HrMonthSalaryItemEntity { MonthSalaryId = objHrMonthSalaryEntity.MonthSalaryId });
                    broker.BeginTransaction();
                    if (objHrMonthSalaryEntity != null) DataAccess.Update(objHrMonthSalaryEntity, broker);
                    if (salaryItems != null && salaryItems.Count > 0)
                    {
                        if (delItems != null && delItems.Count > 0) DataAccess.Delete(delItems);
                        DataAccess.Insert(salaryItems, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }

        }

        public static bool SubmitHrMonthSalary(string salarymonth)
        {
            List<HrMonthSalaryEntity> updateSalarys = DataAccess.Select(new HrMonthSalaryEntity { SalaryMonth = salarymonth, Status = "MSS01" });
            if (updateSalarys != null && updateSalarys.Count > 0)
            {
                foreach (HrMonthSalaryEntity salary in updateSalarys)
                    salary.Status = "MSS02";
            }
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (updateSalarys != null && updateSalarys.Count > 0) DataAccess.Update(updateSalarys, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }

        }


        public static bool ApproveHrMonthSalary(HrMonthSalaryEntity salaryEntity)
        {
            List<HrMonthSalaryEntity> updateSalarys = DataAccess.Select(new HrMonthSalaryEntity { SalaryMonth = salaryEntity.SalaryMonth, Status = "MSS02" });
            if (updateSalarys != null && updateSalarys.Count > 0)
            {
                foreach (HrMonthSalaryEntity salary in updateSalarys)
                {
                    salary.Status = salaryEntity.Status;
                    salary.ApprovedBy = salaryEntity.ApprovedBy;
                    salary.ApprovedDate = salaryEntity.ApprovedDate;
                }
            }
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (updateSalarys != null && updateSalarys.Count > 0) DataAccess.Update(updateSalarys, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }

        }
    }
	
	
}	

